from crewai.tools import tool

import json
from jsonpath import JSONPath
file_path = "/Users/martinzhang/mt-20230221/zxjd-proj-20240405/04-development/python_tool_proj/management_principles.json"

class TextboxTools():
    @tool("获取所有章节标题")
    def get_chapter_titles():
        """获取所有章节标题"""
        chatpters = TextboxTools.get_all_chapters()
        return [chapter['title'] for chapter in chatpters]
        
    @tool("根据章节标题，获取小节标题")
    def get_section_titles_by_chapter(chapter_title: str):
        """根据章节标题，获取小节标题"""
        chapter = TextboxTools.get_chapter_by_title(chapter_title)
        if chapter:
            return [section['title'] for section in chapter.get('sections', [])]
        return []

    def get_chapter_by_title(chapter_title: str):
        chapters = TextboxTools.get_all_chapters()
        return next((chapter for chapter in chapters if chapter['title'] == chapter_title), None)

    def get_all_chapters():
        with open(file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
            jsonpath_expr = JSONPath('$.chapters')
            m = jsonpath_expr.parse(data)
            return m[0]

if __name__ == "__main__":
    # tools = TextboxTools()
    cps = TextboxTools.get_chapter_titles()
    ch = TextboxTools.get_section_titles_by_chapter("第一章管理的历史发展")
    print(cps)